package com.kaifamiao.www.Thread.do0927;

public class Test01 {

    public static void main(String[] args) {
        Num num = new Num();
        Runnable plus = ()->{
          for (int i = 0;i<100;i++){
              try {
                  Thread.sleep(10);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              num.plus();
          }
        };
        Runnable Minus =() ->{
            for (int i = 0;i<100;i++){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num.minus();
            }
        };
        Thread thread = new Thread(plus,"plus1");
        Thread thread1 = new Thread(plus,"plus2");

        Thread thread2 = new Thread(Minus,"minus1");
        Thread thread3 = new Thread(Minus,"minus2");
        thread.start();
        thread1.start();
        thread2.start();
        thread3.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(num.j);
    }
}

class Num{
    public int j = 0;
    public synchronized void plus(){
        System.out.println(Thread.currentThread().getName()+"对j加一后是"+ ++j);

    }
    public synchronized void minus(){
        System.out.println(Thread.currentThread().getName()+"对j减一后是"+ --j);
    }
}